﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Web.UI;

using CommunityServer.Components;
using CommunityServer.Controls;

using CSLive.LiveSearch;

using Control=CSLive.ControlPanel.Editor.Control;

namespace CSLive.Controls
{
    public class SearchBox : WrappedContentBase, ICSLiveControl
    {
        [Browsable( true )]
        [Bindable( false )]
        [Category( "Settings" )]
        public bool UseAJAX
        {
            get { return ( ViewState[ "UseAJAX" ] == null ) ? true : ( bool ) ViewState[ "UseAJAX" ]; }
            set { ViewState[ "UseAJAX" ] = value; }
        }

        #region ICSLiveControl Members

        public Dictionary< string, Control > EditorControls
        {
            get
            {
                Dictionary< string, Control > dictionary;

                dictionary = new Dictionary< string, Control >
                                 {
                                     {
                                         "Live Search Box (AJAX)",
                                         new Control( "Live Search Box (AJAX)",
                                                      "Windows Live Search AJAX Search Box", true,
                                                      "<CSLive:SearchBox runat=\"server\" />" )
                                         },
                                     {
                                         "Live Search Box",
                                         new Control( "Live Search Box",
                                                      "Windows Live Search HTML Search Box", true,
                                                      "<CSLive:SearchBox UseAJAX=\"false\" runat=\"server\" />" )
                                         }
                                 };
                return dictionary;
            }
        }

        public string Name
        {
            get { return "Live Search Box"; }
        }

        public Uri DocsUri
        {
            get { return new Uri( "http://www.codeplex.com/CSLive/Wiki/View.aspx?title=Search%20Controls&referringTitle=CSLive%202.0" ); }
        }

        public string Description
        {
            get { return "Windows Live Search Box, uses AJAX to communicate with Windows Live and get search results"; }
        }

        public string Group
        {
            get { return "Live Search"; }
        }

        #endregion

        protected override void BindDefaultContent( System.Web.UI.Control control, IDataItemContainer dataItemContainer )
        {
            LiveConfiguration config = LiveConfiguration.Instance;
            control.Controls.Add( new LiteralControl( config.getConfigSetting( Name, "Search Form Template" ).Value.ToString() ) );
            if ( UseAJAX )
            {
                control.Controls.Add( new LiteralControl( "  <script type=\"text/javascript\" charset=\"utf-8\">\n" ) );
                control.Controls.Add( new LiteralControl( "	var WLSearchBoxConfiguration=\n" ) );
                control.Controls.Add( new LiteralControl( "	{\n" ) );
                control.Controls.Add( new LiteralControl( "		\"global\":{\n" ) );
                control.Controls.Add( new LiteralControl( "			\"serverDNS\":\"search.live.com\",\n" ) );
                switch ( ( Language ) config.getConfigSetting( Name, "Language" ).Value )
                {
                    case Language.UK_English:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"en-GB\"\n" ) );
                        break;
                    case Language.Australia_English:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"en-AU\"\n" ) );
                        break;
                    case Language.Canada_English:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"en-CA\"\n" ) );
                        break;
                    case Language.Canada_French:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"fr-CA\"\n" ) );
                        break;
                    case Language.China_Chinese:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"zh-CN\"\n" ) );
                        break;
                    case Language.France_French:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"fr-FR\"\n" ) );
                        break;
                    case Language.Germany_German:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"de-DE\"\n" ) );
                        break;
                    case Language.Italy_Italian:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"it-IT\"\n" ) );
                        break;
                    case Language.Japan_Japanese:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"ja-JP\"\n" ) );
                        break;
                    case Language.Korea_Korean:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"ko-KR\"\n" ) );
                        break;
                    case Language.Netherlands_Dutch:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"nl-NL\"\n" ) );
                        break;
                    case Language.Spain_Spanish:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"es-ES\"\n" ) );
                        break;
                    case Language.US_Spanish:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"ES-US\"\n" ) );
                        break;
                    default:
                        control.Controls.Add( new LiteralControl( "			\"market\":\"en-US\"\n" ) );
                        break;
                }
                control.Controls.Add( new LiteralControl( "		},\n" ) );
                control.Controls.Add( new LiteralControl( "		\"appearance\":{\n" ) );
                control.Controls.Add( new LiteralControl( "			\"autoHideTopControl\":false,\n" ) );
                control.Controls.Add( new LiteralControl( "			\"width\":" + config.getConfigSetting( Name, "Pane Width" ).Value + ",\n" ) );
                control.Controls.Add( new LiteralControl( "			\"height\":" + config.getConfigSetting( Name, "Pane Height" ).Value + ",\n" ) );
                control.Controls.Add( new LiteralControl( "			\"theme\":\"" + config.getConfigSetting( Name, "Pane Theme" ).Value + "\"\n" ) );
                control.Controls.Add( new LiteralControl( "		},\n" ) );
                control.Controls.Add( new LiteralControl( "		\"scopes\":[\n" ) );
                if ( ( bool ) config.getConfigSetting( Name, "Enable Site Search" ).Value )
                {
                    control.Controls.Add( new LiteralControl( "			{\n" ) );
                    control.Controls.Add( new LiteralControl( "				\"type\":\"web\",\n" ) );
                    control.Controls.Add( new LiteralControl( "				\"caption\":\"" + Context.Server.HtmlEncode( CSContext.Current.SiteSettings.SiteName ) + "\",\n" ) );
                    control.Controls.Add( new LiteralControl( "				\"searchParam\":\"site:" + Context.Request.Url.Host + "\"\n" ) );
                    control.Controls.Add( new LiteralControl( "			}\n" ) );
                }
                if ( ( bool ) config.getConfigSetting( Name, "Enable Site Search" ).Value && ( bool ) config.getConfigSetting( Name, "Enable Web Search" ).Value )
                    control.Controls.Add( new LiteralControl( "			,\n" ) );
                if ( ( bool ) config.getConfigSetting( Name, "Enable Web Search" ).Value )
                {
                    control.Controls.Add( new LiteralControl( "			{\n" ) );
                    control.Controls.Add( new LiteralControl( "				\"type\":\"web\",\n" ) );
                    control.Controls.Add( new LiteralControl( "				\"caption\":\"&#x57;&#x65;&#x62;\",\n" ) );
                    control.Controls.Add( new LiteralControl( "				\"searchParam\":\"\"\n" ) );
                    control.Controls.Add( new LiteralControl( "			}\n" ) );
                }
                control.Controls.Add( new LiteralControl( "		]\n" ) );
                control.Controls.Add( new LiteralControl( "	}\n" ) );
                control.Controls.Add( new LiteralControl( "	</script>\n" ) );
                control.Controls.Add( new LiteralControl( "   <script type=\"text/javascript\" charset=\"utf-8\" src=\"http://search.live.com/bootstrap.js?market=en-GB&ServId=SearchBox&ServId=SearchBoxWeb&Callback=WLSearchBoxScriptReady\"></script>\n" ) );
            }
            else
            {
                control.Controls.Add( new LiteralControl( "  <script type=\"text/javascript\" charset=\"utf-8\">\n" ) );
                control.Controls.Add( new LiteralControl( "     document.getElementById('WLSearchBoxButton').onclick = function() { \n" ) );
                control.Controls.Add( new LiteralControl( "        if (document.getElementById(\"WLSearchBoxInput\").value != '')\n" ) );
                control.Controls.Add( new LiteralControl( "           window.open('http://search.live.com/results.aspx?q=' + document.getElementById(\"WLSearchBoxInput\").value.replace(\" \", \"+\") + ' site:" + Context.Request.Url.Host + "');\n" ) );
                control.Controls.Add( new LiteralControl( "        return false;\n" ) );
                control.Controls.Add( new LiteralControl( "     };\n" ) );
                control.Controls.Add( new LiteralControl( "     document.getElementById(\"WLSearchBoxInput\").value = '';\n" ) );
                control.Controls.Add( new LiteralControl( "     document.getElementById(\"WLSearchBoxInput\").removeAttribute('disabled');\n" ) );
                control.Controls.Add( new LiteralControl( "	</script>\n" ) );
            }
        }
    }
}